package com.pj.wisdomlogistics.ui.activity;

import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pj.spinnerlib.views.DensityUtil;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.entity.CarPoritionInfo;
import com.pj.wisdomlogistics.mvp.contract.CarFreeContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.CarFreePresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.CarFreeAdapter;
import com.pj.wisdomlogistics.ui.view.CustomAlertDialog;
import com.pj.wisdomlogistics.ui.view.GridSpacingItemDecoration;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FreeCarActivity extends MvpActivity<CarFreePresenter> implements CarFreeContract.View , CarFreeAdapter.onItemClick {
    @BindView(R.id.rv_choose_car)
    RecyclerView mRvChooseCar;

    private CarFreeAdapter mAdapter;
    private List<CarPoritionInfo.CarListBean> mList;

    private View mCurrentView;
    private int state = -1;
    private int position = -1;

    @Override
    public void initView() {
        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.text_blue));
        mRvChooseCar.setLayoutManager(new GridLayoutManager(this , 2 , LinearLayoutManager.VERTICAL , false));
        mRvChooseCar.addItemDecoration(new GridSpacingItemDecoration(2 , DensityUtil.dip2px(this , 20) , false));
        mAdapter = new CarFreeAdapter(this);
        mRvChooseCar.setAdapter(mAdapter);
        mAdapter.setListener(this);

        mPresenter.findCarAndDriver();
        showProgressDialog();
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_free_car;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    public void itemClick(final int position , final int state , final View mView) {
        String msg = state == 0 ? "是否加锁该车辆？" : "是否释放该车辆？";
        this.state = state;
        this.position = position;
        new CustomAlertDialog.Builder(this)
                .setMessage(msg)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        mCurrentView = mView;
                        mPresenter.updateCarState(mList.get(position).getCarID() , state == 0 ? 1 : 0);
                        showProgressDialog();
                    }
                })
                .create()
                .show();
    }

    @Override
    public void onError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this , errorMsg , 1000);
    }

    @Override
    public void onSuccess(CarPoritionInfo mInfo) {
        dismissProgressDialog();
        mAdapter.setList(mInfo.getCarList());
        mList = mInfo.getCarList();
    }

    @Override
    public void onSendSuccess() {
        dismissProgressDialog();
        CustomToast.showToast(this , state == 0 ? "加锁成功" : "释放成功"
                , 1000);
        mCurrentView.setBackgroundResource(state == 1 ? R.drawable.select_car : R.drawable.select_car_2);
        mList.get(position).setState(state == 0 ? 1 : 0);
    }


    @Override
    protected BaseView getBaseView() {
        return this;
    }
}
